class AllDB {
    static tabName;
    db;
    constructor(dbName, tabName, createIndexFn,initFn) {
        this.tabName = tabName;
        var request = window.indexedDB.open(dbName, 1);
        request.onupgradeneeded = event => {
            var db = event.target.result;
            var objectStore;
            if (!db.objectStoreNames.contains(tabName)) {
                objectStore = db.createObjectStore(tabName, { keyPath: "id" });
                if(createIndexFn&& typeof createIndexFn == "function"){
                    createIndexFn(objectStore);
                }
            }
        };

        request.onsuccess = e => {
            this.db = e.target.result;
            if(initFn&& typeof initFn == "function"){
                initFn();
            }
        };

        request.onerror = e => {
            console.log("error");
        };
    }
    getDb(){
        return this.db;
    }
    getCurrentRequest(){
        return this.db
        .transaction([this.tabName], "readwrite")
        .objectStore(this.tabName);
    }
    resultObj(curRequest, success) {
        curRequest.onerror = function(event) {
          console.log("操作失败");
        };
        curRequest.onsuccess = event =>{
          return success(event)
        }
      }
}

export default AllDB;



